//
//  JBWEmoticonPageViewCell.swift
//  新浪微博
//
//  Created by 季伯文 on 2017/7/11.
//  Copyright © 2017年 bowen. All rights reserved.
//

import UIKit

class JBWEmoticonPageViewCell: UICollectionViewCell {
    
    var indexPath: IndexPath?{
        didSet{
            messsgeLabel.text = "第\(indexPath?.section ?? 0)组\n第\(indexPath?.item ?? 0)页"
        }
    }
    //提供属性供外界赋值
    var emoticons: [JBWEmoticonModel]?{
        didSet{
            
            //先隐藏
            for button in buttonList{
                button.isHidden = true
            }
            //遍历emoticons
            for (i, emoticonModel) in emoticons!.enumerated(){
                
                //通过下标获取按钮
                let button = buttonList[i]
                
                //赋值
                button.emoticonModel = emoticonModel
                //显示按钮
                button.isHidden = false
                //判断是否是emoji表情
                if emoticonModel.isEmoji {
                    //如果是emoji 设置为title
                    //获取到code
                    let code = ((emoticonModel.code ?? "") as NSString).emoji()
                    //设置title
                    button.setTitle(code, for: UIControlState.normal)
                    button.setImage(nil, for: UIControlState.normal)
                } else {
                    //如果是图片表情 设置为image
                    let png = emoticonModel.allPath ?? ""
                    
                    let image = UIImage(named: png, in: JBWEmoticonTools.share.emoticonBundle, compatibleWith: nil)
                    button.setImage(image, for: UIControlState.normal)
                    button.setTitle(nil, for: UIControlState.normal)
                    
                    
                }
            }
        }
    }
    
    //保存20个表情按钮的数组
    lazy var buttonList: [JBWEmoticonButton] = [JBWEmoticonButton]()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        //计算按钮的宽度和高度
        let buttonW = (self.frame.width - 10) / CGFloat(JBWEMOTICONMAXCOL)
        let buttonH = (self.frame.height - 20) / CGFloat(JBWEMOTICONMAXROW)
        //遍历保存按钮的数组
        for (i, button) in buttonList.enumerated(){
            //行索引和列索引
            let rowIndex = i / JBWEMOTICONMAXCOL
            let colIndex = i % JBWEMOTICONMAXCOL
            button.frame = CGRect(x: CGFloat(colIndex)*buttonW + 5, y: CGFloat(rowIndex)*buttonH, width: buttonW, height: buttonH)
            
        }
        //设置删除按钮的frame
        deleteButton.frame = CGRect(x: self.frame.width - buttonW - 5, y: buttonH*2, width: buttonW, height: buttonH)
        
    }
    //设置视图
    private func setupUI(){
    
//        backgroundColor = randomColor()
        addChildButton()
        addSubview(deleteButton)
        contentView.addSubview(messsgeLabel)
        
        messsgeLabel.snp.makeConstraints { (make) in
            make.center.equalTo(contentView)
        }
    }
    //懒加载控件
    private lazy var messsgeLabel: UILabel = {
        let lab = UILabel()
        lab.textColor = UIColor.black
        lab.font = UIFont.systemFont(ofSize: 32)
        lab.numberOfLines = 0
        return lab
        
    }()
    
    //删除按钮
    private lazy var deleteButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: #selector(deleteButtonClick), for: UIControlEvents.touchUpInside)
        button.setImage(UIImage(named: "compose_emotion_delete"), for: UIControlState.normal)
        button.setImage(UIImage(named: "compose_emotion_delete_highlighted"), for: UIControlState.highlighted)
        return button
        
    }()
}

//创建20个按钮
extension JBWEmoticonPageViewCell{
    fileprivate func addChildButton(){
        
        //循环
        for _ in 0..<JBWEMOTICONMAXCOUNT{
            //创建button
            let button = JBWEmoticonButton()
            button.addTarget(self, action: #selector(buttonClick), for: UIControlEvents.touchUpInside)
            button.titleLabel?.font = UIFont.systemFont(ofSize: 30)
//            button.backgroundColor = randomColor()
            buttonList.append(button)
            contentView.addSubview(button)
        
            
        }
    }
}

extension JBWEmoticonPageViewCell{
    
    @objc fileprivate func buttonClick(button: JBWEmoticonButton){
        
        //发送通知
        NotificationCenter.default.post(name: JBWEMOTICONBUTTONCLICKNOTI, object: button.emoticonModel)
    }
    
    @objc fileprivate func deleteButtonClick(){
        //发送通知
        NotificationCenter.default.post(name: JBWDELETEBUTTONCLICKNOTI, object:nil)
    }
}
